Here is my code to send data to the server but I always get this Error.
let data = {
"email": "test@gmail.com",
"language": "en",
"country": "en",
"istestentry": false
};
let jsonData = JSON.stringify(data);
let encodedData = encodeURIComponent(jsonData);
let url = "https://example.org/b/receiver/external";
let fetchUrl = url + "?datajsonstr=" + encodedData + "&_=" + (new Date()).valueOf();
let datafetch = fetch(fetchUrl);
const [fetchedData, setFetchedData] = useState('');
async function fetchData(){
const {data} = await axios.post(
datafetch
)
setFetchedData(data)
}
useEffect(() => {
fetchData()
}, [])
const handleSubmit = (event) => {
event.preventDefault();
fetchData()
};
I don't know how to solve this problem.
You are trying to send a promise instead of a stringified data. let datafetch = fetch(fetchUrl); gives you a promise. Also sending data through params is done with get requests. So I assume that you may need to make a get request:
let fetchUrl = url + "?datajsonstr=" + encodedData + "&_=" + (new Date()).valueOf();
const [fetchedData, setFetchedData] = useState('');
async function fetchData(){
const {data} = await axios.get(fetchUrl)
setFetchedData(data)
}
(P.S. get or post depends on how your api handles requests. You can choose the correct one)
I solved it like this. if you don't want to use Axios, you can solve it also like this.
const [dataDone, setDataDone] = useState(false);
useEffect(() => {
let data = {
"email": "test@gmail.at",
"language": "de",
"country": "de",
"istestentry": false
};
let jsonData = JSON.stringify(data);
let encodedData = encodeURIComponent(jsonData);
let url = "https://org.com;
let fetchUrl = url + "?datajsonstr=" + encodedData + "&_=" + (new
Date()).valueOf();
let datafetch = fetch(fetchUrl);
datafetch.then(data => {
setDataDone(true)
})
}, [])